package cn.gorit.content.service.impl;

import cn.gorit.common.enums.ResultEnums;
import cn.gorit.common.lang.Result;
import cn.gorit.content.service.LikeService;
import cn.gorit.entity.content.Like;
import cn.gorit.entity.user.User;
import cn.gorit.dto.content.BlogUpdateDto;
import cn.gorit.mapper.content.LikeMapper;
import cn.gorit.mapper.content.BlogMapper;
import cn.gorit.utils.Page;
import java.lang.Integer;

import cn.gorit.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @version v1.0
 * @Classname LikeServiceImpl
 * @Description 点赞开发
 * @Author CodingGorit
 * @Created on 2021/4/5 20:35
 * @IDLE IntelliJ IDEA
 **/
@Service
public class LikeServiceImpl implements LikeService {

    @Autowired
    private LikeMapper likeMapper;

    @Autowired
    private RestTemplate template;

    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    LoadBalancerClient loadBalancerClient;

    // 添加点赞一定是添加
    @Override
    public Result<Object> insertLike(Like like) {
        // 安全校验，检验是否已经点过赞了
        Integer type = like.getLikeType();
        String id = like.getLikeId();
        like.setId(id + type + like.getUserId());
        likeMapper.insertLike(like);
        // 修改 blog 的点赞数量，增加
        if (type.equals(1)) {
            BlogUpdateDto dto = blogMapper.getBlogUpdate(id);
            dto.setBlogLikes(dto.getBlogLikes() + 1); // 后端不再添加数据
            blogMapper.updateBlogLike(dto);
        }
        // 修改 comment 的点赞数量
        if (type.equals(2)) {
            // 待处理
        }
        return new Result<>(ResultEnums.ADD_SUCCESS.getCode(),ResultEnums.ADD_SUCCESS.getMsg());
    }

    @Override
    public Result<Like> getLikeByUserIdAndBlogId(String userId,String blogId) {
        Like like = likeMapper.getLikeByUserIdAndBlogId(userId, blogId);
        if (like!= null)
            return new Result<>(ResultEnums.QUERY_SUCCESS.getCode(),ResultEnums.QUERY_SUCCESS.getMsg(), like);
        return new Result<>(ResultEnums.NOT_FOUND.getCode(),ResultEnums.NOT_FOUND.getMsg());
    }

    // 可能点赞 也有 可能是取消点赞
    @Override
    public Result<Object> updateLike(Like like) {
        Integer type = like.getLikeType();
        String id = like.getLikeId();
        BlogUpdateDto dto = blogMapper.getBlogUpdate(id);// 先查阅点赞是否为0，如果是0，则调用添加的方法
        like.setId(id + type + like.getUserId());
        likeMapper.updateLike(like);
        // 修改 blog 的点赞数量
        if (type.equals(1)) {
            Integer count = like.getStatus() == 0 ? dto.getBlogLikes() - 1 : dto.getBlogLikes() + 1;
            // redis 持久化处理，定时任务同步
            dto.setBlogLikes(count < 0 ? 0 : count);
            blogMapper.updateBlogLike(dto);
        }
        // 修改 comment 的点赞数量
        if (type.equals(2)) {
            // 待处理
        }
        return new Result<>(ResultEnums.UPDATE_SUCCESS.getCode(),ResultEnums.UPDATE_SUCCESS.getMsg());
    }

    @Override
    public Result<Page<Like>> getLikeList(Page<Like> like) {
        List<Like> likes = likeMapper.getLikeList(like);
        like.setList(likes);
        Integer count = getCountLikeList(like);
        like.setTotalCount(count);
        return new Result<>(ResultEnums.QUERY_SUCCESS.getCode(),ResultEnums.QUERY_SUCCESS.getMsg(),like);
    }

    @Override
    public Integer getCountLikeList(Page<Like> like) {
        return likeMapper.getLikeCount(like);
    }

    // RPC 远程调用
    @Override
    public Result getUserByUserId(String userId) {
        ServiceInstance serviceInstance = loadBalancerClient.choose("cloud-myblog-user-8001");
        String url = "http://" + serviceInstance.getServiceId() + "/api/v1/user/front/info/" + userId;
        String dto = template.getForObject(url,String.class);
        return new Result<>(ResultEnums.QUERY_SUCCESS.getCode(),ResultEnums.QUERY_SUCCESS.getMsg(),dto);
    }
}
